package m1;

/*
    2022/1/20
    9:55
*/

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws Exception, TimeoutException {
        //连接服务器
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection(); //获取链接
        Channel channel = con.createChannel(); //建立通信通道

        //创建队列
        /**
         * queue:队列名称
         * durable:队列是否持久化
         * exclusive:是否是排它队列，单个消费者独占队列
         * autoDelete: 当最后一个消费者断开连接后是否删除队列
         * arguments: 队列的其他属性
         */
        channel.queueDeclare("HelloWorld!",false,false,false,null);

        //发送消息
        channel.basicPublish("","HelloWorld!",null,"Welcome to China!".getBytes());
        System.out.println("消息发送成功~");
        channel.close();
        con.close();
    }
}
